// pages/orderInfo/orderinfodetail.js
// 获取应用实例
const app = getApp()
const $req = require('../../utils/req.js') //接口封装文件
Page({

  /**
   * 页面的初始数据
   */
  data: {
    istrue:false,
    payTime: '请在15分钟内完成支付',
    OrderNo:null,
    info:[],
    Discount:null,
    TotalFee:null,
    goodsList:[],
    goodsListTotalPrice:0,
    finsTotalprice:0,
    buydiscount:null,
    onCheckbox:0,
    token:null,
    backtoruletrue:false
  },
  cancelorder(e){
    let that = this;
    that.setData({
      backtoruletrue:true
    })
  },
  //调用支付接口
  onCheckbox(e){
    let that = this;
      console.log(e)  
  },
  /*打开支付页面*/
  saveOrder(e){
    let that = this;
    let info = that.data.info
    let Details = []
    Details.push({
      Mdate: info.Mdate,
      TimesID: info.Details[0].TimesID,
    });
    let goodsdata = []
    that.data.goodsList.forEach((item, index) => {
        goodsdata.push({
          GoodsID: item.GoodsID,
          Nums: item.num
        })
    })
    let data = {
      Token: wx.getStorageSync("token"),
      OrderNo: info.OrderNo, //	string	订单编号(新订单为空)
      RoomID: info.RoomID,
      Mdate: info.Mdate, //date	会议室预订日期(开始日期）
      MdateTo: info.MdateTo, //	date	会议室预订日期(结束日期）
      Details: Details, //	Json	列表
      Goods: goodsdata, // Goods 商品
      Contract:info.Contract?info.Contract:'',
      Descr:info.Descr?info.Descr:'',
      MeetName:info.MeetName?info.MeetName:'',
    }
    let jsonObj = JSON.stringify(data)
    that.setData({
      istrue:true
    })
  },
  //获取订单详情
  getorderinfolist(e){
    let _this = this
    let OrderNo = _this.data.OrderNo
    let token = wx.getStorageSync('token')
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
       //请求后台的分页数据
       cmd:'GetOrderInfo',
       token:token,
       OrderNo:OrderNo
      },
      method: "get",
      success(res) {
       var data = res.data.DATA;
       var goodsList = data[0].GoodsList
       console.log(data);
       let goodsListTotalPrice = 0
       goodsList.forEach((item, index) => {
        item.num = item.Nums
        item.totalPrice = item.Price * item.num
        goodsListTotalPrice += parseFloat(item.totalPrice)
      })
      //console.log(goodsList)
       _this.setData({
        info:data[0],
        finsTotalprice:data[0].RoomFeeDis*1+data[0].GoodsFee,
        Discount:_this.data.Discount*10,
        goodsList:goodsList,
        buydiscount:data[0].RoomFee+'*'+_this.data.Discount+'='+data[0].RoomFeeDis,
        goodsListTotalPrice:goodsListTotalPrice
      })
      }
   })
  },
  //返回上一页
  navigateBack() {
    wx.navigateBack({
      delta: 2
    })
  },
  //商品添加修改
  changeGoodsNum(e) {
    let _this = this
    let data = e.currentTarget.dataset
    let goodsList = _this.data.goodsList
    
    let goods = goodsList[data.index]
 
    switch (data.type) {
      case "+":
        goods.num += 1
        break
      case "-":
        goods.num -= 1
        break
    }
    if (goods.num < 0) {
      goods.num = 0
    }

    goods.totalPrice = (goods.num * goods.Price).toFixed(2)
    _this.goodsListTotalPrice()
    _this.finsTotalprice()
    _this.setData({
      goodsList: goodsList,
    })
    
  },
  goodsListTotalPrice() {
    let _this = this
    let goodsListTotalPrice = 0
    let goodsList = _this.data.goodsList
    goodsList.forEach((item, index) => {
      goodsListTotalPrice += parseFloat(item.totalPrice)
    })
    console.log(goodsList)
    console.log(_this.data.info)
    _this.setData({
      goodsListTotalPrice: goodsListTotalPrice.toFixed(2),
      goodsList:goodsList
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    console.log(options)
    //获得当前会议室的折扣
    let  token =  wx.getStorageSync('token')
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        cmd: 'GetRoomDiscount',
        token: token,
        RoomID: options.id
      },
      method: "get",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        _this.setData({
          Discount:res.data.DATA.Discount
        })
      }
    })
    //页面加载获得页面信息和用户缓存
    _this.setData({
      OrderNo:options.OrderNo,
      combined:options.TotalFee,
      token:wx.getStorageSync('token')
    })
    _this.getorderinfolist(options.id)
    _this.goodsListTotalPrice()
  },

  onShow(e){
    let  userInfo =  wx.getStorageSync('userInfo')
    if(userInfo){
    console.log(111)
   }else{
     wx.navigateTo({
       url: '/pages/login/login',
     })
   }
  
    },
  // 计算总金额
  totalPrice() {
    let _this = this
    let roomTiemsCopy = Object.create(_this.data.roomTimes)
    let totalPrice = 0
    roomTiemsCopy.forEach((item, index) => {
      if (item.checkFlag) {
        totalPrice += item.Price
      }
    })
    _this.setData({
      totalPrice: (totalPrice).toFixed(2),
      // disAmount: (totalPrice * wx.getStorageSync('userInfo').Discount).toFixed(2),
    })
  },

  changeGoodsNum(e) {
    let _this = this
    let data = e.currentTarget.dataset
    let goodsList = _this.data.goodsList
    let goods = goodsList[data.index]
    switch (data.type) {
      case "+":
        goods.num += 1
        break
      case "-":
        goods.num -= 1
        break
    }
    if (goods.num < 0) {
      goods.num = 0
    }
    goods.totalPrice = (goods.num * goods.Price).toFixed(2)
    _this.goodsListTotalPrice()
    _this.finsTotalprice()
    _this.setData({
      goodsList: goodsList,
    })
  },

  finsTotalprice() {
    let _this = this
    _this.setData({
      finsTotalprice: (parseFloat(_this.data.info.RoomFeeDis) + parseFloat(_this.data.goodsListTotalPrice)).toFixed(2),
    })
  }
})